package com.blrs.service.enterprise.quotation.impl;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.blrs.common.core.domain.entity.SysDept;
import com.blrs.common.utils.SecurityUtils;
import com.blrs.domain.QuotationProductMessage;
import com.blrs.domain.vo.QuotationProductMessageVo;
import com.blrs.mapper.BusinessOpportunityInformationMapper;
import com.blrs.mapper.QuotationProductMessageMapper;
import com.blrs.system.mapper.SysDeptMapper;
import com.blrs.system.mapper.SysUploadFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.blrs.mapper.QuotationInfoMapper;
import com.blrs.domain.QuotationInfo;
import com.blrs.service.enterprise.quotation.IQuotationInfoService;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-25
 */
@Service
public class QuotationInfoServiceImpl implements IQuotationInfoService 
{
    @Autowired
    private QuotationInfoMapper quotationInfoMapper;

    @Autowired
    private QuotationProductMessageMapper quotationProductMessageMapper;
    @Autowired
    private BusinessOpportunityInformationMapper businessOpportunityInformationMapper;

    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysUploadFileMapper sysUploadFileMapper;


    /**
     * 查询【请填写功能名称】
     * 
     * @param quotationId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public QuotationInfo selectQuotationInfoByQuotationId(String quotationId)
    {
        QuotationInfo quotationInfo = quotationInfoMapper.selectQuotationInfoByQuotationId(quotationId);
        return quotationInfo;
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param quotationInfo 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<QuotationInfo> selectQuotationInfoList(QuotationInfo quotationInfo)
    {
        List<QuotationInfo> quotationInfos = quotationInfoMapper.selectQuotationInfoList(quotationInfo);
        quotationInfos.forEach(item->{
             item.setBusinessName( businessOpportunityInformationMapper.selectBusinessOpportunityInformationByBusinessId(item.getBusinessId()).getBusinessName());
             int count=sysUploadFileMapper.selectUrlByCodeCount("businessinfo", item.getUploadFile());
             item.setUrl(count > 0 ? String.valueOf(count) : null);
        }
        );
        return quotationInfos;
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param quotationInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertQuotationInfo(QuotationInfo quotationInfo)
    {
        return quotationInfoMapper.insertQuotationInfo(quotationInfo);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param quotationInfo 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateQuotationInfo(QuotationInfo quotationInfo)
    {
        return quotationInfoMapper.updateQuotationInfo(quotationInfo);
    }

    /**
     * 批量删除【请填写功能名称】
     * 
     * @param quotationIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteQuotationInfoByQuotationIds(String[] quotationIds)
    {
        return quotationInfoMapper.deleteQuotationInfoByQuotationIds(quotationIds);
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param quotationId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteQuotationInfoByQuotationId(String quotationId)
    {
        return quotationInfoMapper.deleteQuotationInfoByQuotationId(quotationId);
    }

    @Override
    public int saveQuotationProductMessage(QuotationProductMessageVo quotationProductMessageVo) {
        int count = 0;
        QuotationInfo quotationInfo = new QuotationInfo();
//        quotationInfo.setApprovalNumber(quotationProductMessageVo.getApprovalNumber());
//        List<QuotationInfo> quotationInfos = quotationInfoMapper.selectQuotationInfoList(quotationInfo);
//        if(quotationInfos.size()>0){
//            throw new RuntimeException("该立项编号已存在！");
//        }else{
//
//        }
        QuotationInfo info = new QuotationInfo();
        info.setQuotationId(quotationProductMessageVo.getQuotationId());
        info.setBusinessId(quotationProductMessageVo.getBusinessId());
        info.setApprovalNumber(quotationProductMessageVo.getApprovalNumber());
        info.setCompanyName(quotationProductMessageVo.getCompanyName());
        info.setCompanyId(quotationProductMessageVo.getCompanyId());
        info.setEmployeeNumber(quotationProductMessageVo.getEmployeeNumber());
        info.setAddress(quotationProductMessageVo.getAddress());
        info.setCertificateAuthority(quotationProductMessageVo.getCertificateAuthority());
        info.setContacts(quotationProductMessageVo.getContacts());
        info.setCustomerDemand(quotationProductMessageVo.getCustomerDemand());
        info.setUnit(quotationProductMessageVo.getUnit());
        info.setIsSubmit(quotationProductMessageVo.getIsSubmit());
        info.setQuotationDescription(quotationProductMessageVo.getQuotationDescription());
        info.setPaymentMethod(quotationProductMessageVo.getPaymentMethod());
        info.setAfterService(quotationProductMessageVo.getAfterService());
        info.setTips(quotationProductMessageVo.getTips());
        info.setApprovalId(quotationProductMessageVo.getApprovalId());
        info.setApprovalName(quotationProductMessageVo.getApprovalName());
        info.setApprovalResults(quotationProductMessageVo.getApprovalResults());
        info.setApprovalInformation(quotationProductMessageVo.getApprovalInformation());
        info.setCreationTime(new Timestamp(new Date().getTime()));
        info.setOperateTime(new Timestamp(new Date().getTime()));
        info.setOperateId(SecurityUtils.getUserId().toString());
        info.setOperateName(SecurityUtils.getLoginUser().getUser().getNickName());
        info.setVersion(quotationProductMessageVo.getVersion());
        info.setTransactionAmount(quotationProductMessageVo.getTransactionAmount());
        info.setTransactionTime(quotationProductMessageVo.getTransactionTime());
        info.setUploadFile(quotationProductMessageVo.getUploadFile());
        info.setBelongTo(SecurityUtils.getOrgId());
        count = quotationInfoMapper.insertQuotationInfo(info);
        //产品信息
//        List<QuotationProductMessage> messageList = quotationProductMessageVo.getProductMessageList();
//        for(int i = 0;i<messageList.size();i++){
//            QuotationProductMessage productMessage = new QuotationProductMessage();
//            productMessage.setId(UUID.randomUUID().toString());
//            productMessage.setQuotationId(quotationProductMessageVo.getQuotationId());
//            productMessage.setSystemType(messageList.get(i).getSystemType());
//            productMessage.setFees(messageList.get(i).getFees());
//            productMessage.setNotes(messageList.get(i).getNotes());
//            quotationProductMessageMapper.insertQuotationProductMessage(productMessage);
//        }
        return count;
    }

    @Override
    public int updateQuotationProductMessage(QuotationProductMessageVo quotationProductMessageVo) {
        int count = 0;
        QuotationInfo quotationInfo = quotationInfoMapper.selectQuotationInfoByQuotationId(quotationProductMessageVo.getQuotationId());
        if(quotationInfo==null){
            count = saveQuotationProductMessage(quotationProductMessageVo);
        }else{
            quotationProductMessageMapper.deleteQuotationProductMessageByQuotationId(quotationProductMessageVo.getQuotationId());
            quotationInfo.setApprovalNumber(quotationProductMessageVo.getApprovalNumber());
            quotationInfo.setEmployeeNumber(quotationProductMessageVo.getEmployeeNumber());
            quotationInfo.setCertificateAuthority(quotationProductMessageVo.getCertificateAuthority());
            quotationInfo.setContacts(quotationProductMessageVo.getContacts());
            quotationInfo.setCustomerDemand(quotationProductMessageVo.getCustomerDemand());
            quotationInfo.setUnit(quotationProductMessageVo.getUnit());
            quotationInfo.setQuotationDescription(quotationProductMessageVo.getQuotationDescription());
            quotationInfo.setPaymentMethod(quotationProductMessageVo.getPaymentMethod());
            quotationInfo.setAfterService(quotationProductMessageVo.getAfterService());
            quotationInfo.setTips(quotationProductMessageVo.getTips());
            quotationInfo.setApprovalId(quotationProductMessageVo.getApprovalId());
            quotationInfo.setApprovalName(quotationProductMessageVo.getApprovalName());
            quotationInfo.setApprovalResults(quotationProductMessageVo.getApprovalResults());
            quotationInfo.setIsSubmit(quotationProductMessageVo.getIsSubmit());
            quotationInfo.setApprovalInformation(quotationProductMessageVo.getApprovalInformation());
            quotationInfo.setOperateTime(new Timestamp(new Date().getTime()));
            quotationInfo.setOperateId(SecurityUtils.getUserId().toString());
            count = quotationInfoMapper.updateQuotationInfo(quotationInfo);
            List<QuotationProductMessage> messageList = quotationProductMessageVo.getProductMessageList();
            for(int i = 0;i<messageList.size();i++){
                QuotationProductMessage productMessage = new QuotationProductMessage();
                productMessage.setId(UUID.randomUUID().toString());
                productMessage.setQuotationId(quotationProductMessageVo.getQuotationId());
                productMessage.setSystemType(messageList.get(i).getSystemType());
                productMessage.setFees(messageList.get(i).getFees());
                productMessage.setNotes(messageList.get(i).getNotes());
                quotationProductMessageMapper.insertQuotationProductMessage(productMessage);
            }
        }
        return count;
    }

    @Override
    public List<SysDept> selectDeptInfoByUserId(SysDept sysDept) {
        SysDept sysDept1 = sysDeptMapper.selectDeptById(SecurityUtils.getOrgId());
        List<SysDept> deptList = sysDeptMapper.selectDeptList(sysDept1);
        return deptList;
    }


}
